package com.glory.dgcpcpda.activity.recipe;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.TempRecipeListAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.TempRecipeEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * FileName: TempRecipeActivity
 * Author: XJH
 * Date: 2021/4/9 10:34
 * Description:
 * History:
 */
public class TempRecipeActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etEqpID)
    FreeUI_ClearEditText etEqpID;
    @BindView(R.id.etBDNum)
    FreeUI_ClearEditText etBDNum;
    @BindView(R.id.tvChipName)
    TextView tvChipName;
    @BindView(R.id.tvCustomCode)
    TextView tvCustomCode;
    @BindView(R.id.tvProduct)
    TextView tvProduct;
    @BindView(R.id.tvPackageType)
    TextView tvPackageType;
    @BindView(R.id.tvWrie)
    TextView tvWrie;
    @BindView(R.id.ivWrie)
    ImageView ivWrie;
    @BindView(R.id.flWrie)
    FrameLayout flWrie;
    @BindView(R.id.list_tv1)
    TextView listTv1;
    @BindView(R.id.list_tv2)
    TextView listTv2;
    @BindView(R.id.list_tv3)
    TextView listTv3;
    @BindView(R.id.list_tv4)
    TextView listTv4;
    @BindView(R.id.rvRecipeList)
    RecyclerView rvRecipeList;
    @BindView(R.id.tvDownLoad)
    TextView tvDownLoad;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    private int mID = -1;
    private TempRecipeListAdapter mAdapter;
    private List<String> mWrieList = new ArrayList<>();
    private FreeUI_GeneralPop mWirePop;
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_recipe_temp;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvTitle.setText("临时Recipe激活");
        tvBaseSubmit.setVisibility(View.GONE);
        listTv1.setText("设备编号");
        listTv2.setText("Recipe名称");
        listTv3.setText("Recipe版本");
        listTv4.setText("版本类型");
        initRecyclerView(rvRecipeList,this);
        mAdapter = new TempRecipeListAdapter(null, (recipeName, id) -> {
            mID = id;
            if (recipeName.length() > 9){
                String substring = recipeName.substring(0, 9);
                String lastString = recipeName.substring(9);
                if (lastString.contains("-")){
                    etBDNum.setText(substring + lastString.split("-")[0]);
                }else {
                    etBDNum.setText(recipeName);
                }
            }else {
                etBDNum.setText(recipeName);
            }
//            if (etBDNum.getText().toString().length() > 9) {
//                etBDNum.setText(etBDNum.getText().toString().substring(0,9));
//            }
            requestBdNumInfo();
        });
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mAdapter.isFirstOnly(true);
        rvRecipeList.setAdapter(mAdapter);
        initEdit(etEqpID, new editTextListener() {
            @Override
            public void onTextChangeListener(String s) {
                FreeUI_EntityCallBack<BaseResponseBean<TempRecipeEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<TempRecipeEntity>>
                        (new TypeToken<BaseResponseBean<TempRecipeEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<TempRecipeEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            List<TempRecipeEntity.TempRecipeInfoEntity> list = response.body().getResponse().getBody().getList();
                            if (list.size() <= 0){
                                showToast("暂无Recipe信息",false);
                                return;
                            }
                            mAdapter.setNewData(list);
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<TempRecipeEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "getRecipeByStatus");
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("total",13);
                map.addProperty("pageNo",1);
                map.addProperty("pageSize",50);
                map.addProperty("sort","createTime");
                map.addProperty("order","descending");
                map.addProperty("equipmentId",s);
                map.addProperty("status",-1);
                map.addProperty("procedure","WB");
                NetUtils.requestPostNet("getRecipeByStatus","RMS/getRecipeByStatus",this,map,callBack);

            }
        });

        initEdit(etBDNum, s -> requestBdNumInfo());
    }

    private void requestBdNumInfo() {
        FreeUI_EntityCallBack<BaseResponseBean<TempRecipeEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<TempRecipeEntity>>
                (new TypeToken<BaseResponseBean<TempRecipeEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<TempRecipeEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    TempRecipeEntity body = response.body().getResponse().getBody();
                    tvChipName.setText(body.getCHIPNAME());
                    tvProduct.setText(body.getPRODUCTMODEL());
                    tvCustomCode.setText(body.getCUSTOMERCODE());
                    tvPackageType.setText(body.getPACKAGETYPE());
                    mWrieList.clear();
                    mWrieList.addAll(body.getWIRESPECS());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<TempRecipeEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "CPC.BDNAMEQUERY");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","QUERY");
        map.addProperty("BDNAME",getText(etBDNum));
        NetUtils.requestPostNet("CPC.BDNAMEQUERY","RMS/BDNAMEQUERY",this,map,callBack);
    }

    @Override
    protected void initData() {
    }

    @OnClick({R.id.flWrie, R.id.tvDownLoad})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.flWrie:
                if (mWrieList.size() <= 0){
                    showToast("未获取到丝材规格列表",false);
                    return;
                }
                mWirePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flWrie.getWidth(), mWrieList, ivWrie, (codeS, i) -> {
                    tvWrie.setText(codeS);
                });

                if (mWirePop.isShowing()) {
                    mWirePop.dismiss();
                }else {
                    FreeApi_PopUtils.setArrowShow(ivWrie);
                    mWirePop.showAsDropDown(flWrie, 0, 0);
                }
                break;
            case R.id.tvDownLoad:
                if (mID == -1){
                    showToast("请选中Recipe",false);
                    return;
                }

                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        FreeUI_EntityCallBack<BaseResponseBean<TempRecipeEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<TempRecipeEntity>>
                                (new TypeToken<BaseResponseBean<TempRecipeEntity>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<TempRecipeEntity>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                                    showConfirmDialog("激活成功");
                                    etEqpID.setText("");
                                    etBDNum.setText("");
                                    tvChipName.setText("");
                                    tvCustomCode.setText("");
                                    tvProduct.setText("");
                                    tvPackageType.setText("");
                                    tvWrie.setText("");
                                    mWrieList.clear();
                                    mID = -1;
                                    mAdapter.setNewData(null);
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<TempRecipeEntity>> response) {
                                super.onError(response);
                                loadError(response.getException(), "updateRecipeStatus");
                            }
                        };
                        JsonObject map = new JsonObject();
                        map.addProperty("id",mID);
                        map.addProperty("pkgType",tvPackageType.getText().toString());
                        map.addProperty("wireType",tvWrie.getText().toString());
                        map.addProperty("bdId",etBDNum.getText().toString());
                        map.addProperty("deviceName",tvProduct.getText().toString());
                        map.addProperty("chipName",tvChipName.getText().toString());
                        map.addProperty("customerCode",tvCustomCode.getText().toString());
                        NetUtils.requestPostNet("updateRecipeStatus","RMS/updateRecipeStatus",this,map,callBack);
                    }
                });
                break;
        }
    }
}
